package com.ld.hpfs.web.controller;


import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ld.hpfs.common.jackson.JackJson;
import com.ld.hpfs.common.utils.Const;
import com.ld.hpfs.pojo.ExtPager;
import com.ld.hpfs.pojo.User;
import com.ld.hpfs.service.BackUpService;
import com.ld.hpfs.web.interseptor.WebConstants;

@Controller
public class BackUpController {
	private static final Logger logger = LoggerFactory.getLogger(BackUpController.class);

	@Autowired
	private BackUpService backUpService;		
	
	@RequestMapping(value = "/getBackUpInfo")
    public void getBackUpInfo(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer) {
		response.setContentType("text/plain;charset=UTF-8");
		try {
			HashMap<String,Object> result = new HashMap<String,Object>();	 
			ArrayList<HashMap<String,Object>> logs =backUpService.getBackUpInfo();	
			result.put("success", true);
			if (logs != null && logs.size() > 0) {
                result.put("data", logs);                  
            } else {
                result.put("data", "");
            }
			String returnMsg = JackJson.fromObjectToJson(result);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
    }
	
	/*
	 * 启动setBackUp
	 */
	@RequestMapping(value = "/setBackUp")
    public void setBackUp(HttpSession session, HttpServletRequest request,
            HttpServletResponse response, PrintWriter writer) {
		HashMap<String,Object> result = new HashMap<String,Object>();
		response.setContentType("text/plain;charset=UTF-8");
		try {
			String lang = session.getAttribute(WebConstants.CURRENT_LANG).toString();
			StringBuilder impUrl = new StringBuilder();
		 		 impUrl.append(Const.PROPERTIES.getProperty("import.server.url"))
		 		 .append(Const.PROPERTIES.getProperty("import.server.setbackup_method")).append("?lang=").append(lang);
			 logger.debug("Import URL = {}", impUrl);
			 HttpClient httpclient = new DefaultHttpClient();
			 HttpGet httpget = new HttpGet(impUrl.toString());
			 HttpResponse response1 = httpclient.execute(httpget);
			 HttpEntity entity = response1.getEntity();
			 if (entity != null) {
				 InputStream instream = entity.getContent();
				 try {
					 String res = EntityUtils.toString(entity);
					 logger.debug(res);
					 Map<String, Object> retMap = JackJson.fromJsonToObject(res, Map.class);
					 String resultCode = retMap.get("ResultCode").toString();                   
					 if(resultCode.equals("0")){
						 result.put("resultcode",0);
					 }else{
					  String resultMsg = retMap.get("ResultMsg").toString();
					  result.put("resultcode",resultMsg);
					 }	
				}finally{
					instream.close();
				}
		    } 
			 String returnMsg = JackJson.fromObjectToJson(result);
				logger.debug("{}", returnMsg);
				writer.print(returnMsg);
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
    }
	
	
	
	
}